home *** CD-ROM | disk | FTP | other *** search
- Path: newsfeed.internetmci.com!panix!usenet
- From: Jeffrey O. Katz <off@panix.com>
- Newsgroups: comp.lang.c++
- Subject: Re: How best to handle constructor failure?
- Date: 5 Jan 1996 08:15:53 GMT
- Organization: Scientific Consultant Services, Inc.
- Message-ID: <4cimnp$hne@news1.panix.com>
- References: <4c36gl$5e5@nnrp1.news.primenet.com>
- NNTP-Posting-Host: off.dialup.access.net
- X-Newsreader: SPRY News 3.03 (SPRY, Inc.)
-
- > Robert DiFalco <difalco@primenet.com> writes:
- > >
- > > My problem is how to best test for constructor success (sufficient memory,
- > > successful allocations) or failure (insuficient memory condition). In straight C, I would
- > > simply try to allocate my arrays, and, if unable to, I would free any that were allocated
- > > and then return a NULL pointer to my structure. In C++, however, constructors do
- > > not have a return value - so I cannot test for a bad return value - and, sometimes
- > > they are called by the compiler to generate temporaries, for example when operators
- > > are overloaded - another place where there may be a not-enough-memory situation.
- > >
- >
- > Is there something wrong with C++ exceptions? If you are worried about
- > memory leaks, just use auto pointers on your dynamic arrays. I don't know
- > what your code is like, but say you have something like one of the
- > following:
- >
- > SomeClass::SomeClass( ... )
- > {
- > try
- > {
- > auto_ptr<SomeArray> a1 = new SomeArray();
- > auto_ptr<SomeArray> a2 = new SomeItem[ SOME_AMOUNT ];
- > .
- > . Maybe you even have code here where you raise the exception
- > .
- > }
- > catch ( exception x )
- > {
- > .
- > . <handle failure here>
- > .
- > }
- > }
- >
- > By using the auto_ptr's, when the stack is cleaned-up, the objects
- > <a1> and <a2> point to will be deleted in the auto_ptr destructor. If you
- > want, you can get the try...catch logic out of your constructor and put
- > it around the calls to this constructor. In any event, I'm not advocating
- > that you copy this code into your program, you really need to develop an
- > exception strategy, but this should point you in the right direction
- > (unless I misunderstood your question).
- >
- > Robert
- >
- >>>>
-
- Yes, using auto pointers will avoid memory leaks from un-destructed objects.
- The problem, however, is how to alter the sequence of code existing OUTSIDE of
- the constructor, based on the success or failure of the constructor.
-
- In addition, we cannot use exceptions for a variety of reasons (e.g., some
- functions must be exported from a DLL using a straight C or PASCAL calling
- convention, and errors need to be handled by a calling application written in
- another language), and, even if we could, it appears that we would still need
- to use a longjmp() inside the try {...} in order to get out of the constructor so as
- to be able to bypass the code that follows the constructor in the function. Using
- longjmp(), an initializer method that is called seperately from the constructor,
- or various "test" methods (a method that returns a success/failure value) makes
- code kludgy and inelegant. I was wondering what the "correct" approach (if
- there is such a thing) for handling the problem was.
-
- To make the problem a little clearer, what we need to do is something like this:
-
- int MyFunction (int n, float y, ...) {
- MyClass mc(n);
- int return_code;
- if (constructor failed entirely) {
- rc = STACK_OVERFLOW;
- } else if (allocations internal to the constructor failed) {
- rc = INSUFFICIENT_HEAP;
- } else {
- ... lots of code ...
- rc = SUCCESS;
- }
- return rc;
- // the implicitly called destructor will perform any needed cleanup
- }
-
- I noticed that there was another thread on constructors that was recently posted,
- where a suggestion was made to use a seperate initialization function (what I called
- a "test" method"). I have done this upon occasion, but I need to overload operators,
- and hence there will be compiler-generated, implicit constructor calls which would
- not include execution of the seperate initializer method! The issue of dealing with
- constructors that can fail appears to be a complex one. Perhaps a good thread
- will start on the subject!.
-
- Jeff
-
-
-